package test.src.util;

import java.text.NumberFormat;
import java.util.*;

public class RandomAdd {
    /**
     * 随机器
     */
    private static Random RANDOM = new Random(93285);

    public static <T> T getItem(List<T> items) {
        return items.get(RANDOM.nextInt(items.size()));
    }

    public static long getItem(long[] items) {
        return items[RANDOM.nextInt(items.length)];
    }

    public static float getItem(float[] items) {
        return items[RANDOM.nextInt(items.length)];
    }

    public static double getItem(double[] items) {
        return items[RANDOM.nextInt(items.length)];
    }

    public static int getItem(int[] items) {
        return items[RANDOM.nextInt(items.length)];
    }

    public static String getItem(String[] items) {
        return items[RANDOM.nextInt(items.length)];
    }

    /**
     * 姓
     */
    private static String FIRSTNAME = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘缪干解应宗宣丁贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄魏加封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘姜詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲台从鄂索咸籍赖卓蔺屠蒙池乔阴郁胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庚终暨居衡步都耿满弘匡国文寇广禄阙东殴殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后江红游竺权逯盖益桓公万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于仲孙太叔申屠公孙乐正轩辕令狐钟离闾丘长孙慕容鲜于宇文司徒司空亓官司寇仉督子车颛孙端木巫马公西漆雕乐正壤驷公良拓拔夹谷宰父谷粱晋楚阎法汝鄢涂钦段干百里东郭南门呼延归海羊舌微生岳帅缑亢况后有琴梁丘左丘东门西门商牟佘佴伯赏南宫墨哈谯笪年爱阳佟第五言福百家姓续";

    /**
     * 女生名
     */
    private static String GIRL = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";

    /**
     * 男生名
     */
    private static String BOY = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";


    /**
     * 院系名
     */
    private static String[] FACULTY = {"信息技术与管理学院", "会计学院", "财政金融学院", "工商管理学院", "外国语学院", "人文与艺术学院"};

    /**
     * 各院系专业
     */
    public static Map<String, String[]> MAJOR = new HashMap<>();

    static {
        MAJOR.put("信息技术与管理学院", new String[]{"计算机科学与技术", "电子商务", "人工智能", "信息管理与信息系统", "电子信息工程"});
        MAJOR.put("会计学院", new String[]{"会计学", "财务管理", "审计学"});
        MAJOR.put("财政金融学院", new String[]{"金融学", "税收学"});
        MAJOR.put("工商管理学院", new String[]{"人力资源管理", "市场营销", "物流管理", "工商管理", "供应链管理"});
        MAJOR.put("外国语学院", new String[]{"英语", "商务英语", "翻译", "日语"});
        MAJOR.put("人文与艺术学院", new String[]{"网络与新媒体", "文化产业管理", "旅游管理"});

    }

    //设置各院系各专业开始学号
    private static Map<String, Map<String, Integer>> startSnoTable = new HashMap<>();

    static {
        for (String fac : FACULTY) {
            Map<String, Integer> table = new HashMap<>();
            String[] majors = MAJOR.get(fac);
            for (int i = 0; i < majors.length; i++) {
                table.put(majors[i], 1);
            }
            startSnoTable.put(fac, table);
        }
    }

    /**
     * 生成中文名
     *
     * @return 中文名
     */
    public static String getChineseName() {
        int index = getNum(0, FIRSTNAME.length() - 1);
        String first = FIRSTNAME.substring(index, index + 1);
        int sex = getNum(0, 1);
        String str = BOY;
        int length = BOY.length();

        if (sex == 0) {
            str = GIRL;
            length = GIRL.length();
        }

        index = getNum(0, length - 1);
        String second = str.substring(index, index + 1);
        int hasThird = getNum(0, 1);
        String third = "";

        if (hasThird == 1) {
            index = getNum(0, length - 1);
            third = str.substring(index, index + 1);
        }

        return first + second + third;
    }

    public static String[] getChineseName(boolean isMale) {
        int index = getNum(0, FIRSTNAME.length() - 1);
        String first = FIRSTNAME.substring(index, index + 1);
        int sex = getNum(0, 1);
        String gender;
        String str;
        int length;

        if (isMale) {
            str = BOY;
            length = BOY.length();
            gender = "男";
        } else {
            str = GIRL;
            length = GIRL.length();
            gender = "女";
        }

        index = getNum(0, length - 1);
        String second = str.substring(index, index + 1);
        int hasThird = getNum(0, 1);
        String third = "";

        if (hasThird == 1) {
            index = getNum(0, length - 1);
            third = str.substring(index, index + 1);
        }

        String fullName = first + second + third;
        return new String[]{gender, fullName};
    }

    private static int getNum(int i, int i1) {
        return new Random().nextInt(i1 - i + 1) + i;
    }

    private static String getFaculty() {
        int index = new Random().nextInt(FACULTY.length);
        return FACULTY[index];
    }

    private static int getFacultyIndex(String faculty) {
        for (int i = 0; i < FACULTY.length; i++) {
            if (faculty.equals(FACULTY[i])) {
                return i + 1;
            }
        }
        return -1; //值不存在的情况
    }

    private static int getMajorIndex(String faculty, String major) {
        String[] majors = MAJOR.get(faculty);
        for (int i = 0; i < majors.length; i++) {
            if (major.equals(majors[i])) return i + 1;
        }
        return -1; //值不存在的情况
    }

    private static String getMajor(String faculty) {
        String[] majors = MAJOR.get(faculty);
        int index = new Random().nextInt(majors.length);
        return majors[index];
    }

    private static String getStudentClass(String faculty, String major) {
        Map<String, Integer> map = startSnoTable.get(faculty);
        int majorNumbers = map.get(major);
        return getTwoNumber(((majorNumbers - 1) / 50 + 1)) + getTwoNumber((majorNumbers - 1) % 50 + 1);
    }

    private static String getSno(String faculty, String major) {
        Map<String, Integer> majorTable = startSnoTable.get(faculty);
        int startSno = majorTable.get(major);
        String out = "2020" + getTwoNumber(getFacultyIndex(faculty)) + getTwoNumber(getMajorIndex(faculty, major)) + getStudentClass(faculty, major);
        startSno++;
        majorTable.put(major, startSno);
        startSnoTable.put(faculty, majorTable);
        return out;
    }

    private static String getBornDay() {
        String[] bornYears = {"2000", "2001", "2001", "2002", "2002", "2002", "2003", "2003"};
        String[] bornMonth = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        String bornDayOfMonth = getTwoNumber(getNum(1, 28));
        String bornYear = getItem(bornYears);
        String bornMonthOfYear = getItem(bornMonth);
        return bornYear + "-" + bornMonthOfYear + "-" + bornDayOfMonth;
    }

    //整数不足两位时补足前导0并传回其字符串
    public static String getTwoNumber(int number) {
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(2);
        formatter.setGroupingUsed(false);
        return formatter.format(number);
    }

    public static String getFourNumber(int number) {
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        return formatter.format(number);
    }

    public static String[] getStudent() {
        String[] genderList = new String[]{"男", "女"};
        String gender = getItem(genderList);
        boolean isBoy = gender.equals("男");
        String[] genderAndName = getChineseName(isBoy);
        String name = genderAndName[1];
        String faculty = getFaculty();
        String major = getMajor(faculty);
        int classNumber = Integer.parseInt(getStudentClass(faculty, major)) / 100;
        String bornDay = getBornDay();
        String sno = getSno(faculty, major);
        return new String[]{sno, name, gender, faculty, major, String.valueOf(classNumber), bornDay};
    }

    public static int[] getTestGrade(int num) {
        int[] resultSet = new int[num];
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            int tempInt = (int)random.nextGaussian(80, 5);
            if (tempInt > 100) tempInt = (int)random.nextGaussian(80, 5);
            resultSet[i] = tempInt;
        }
        return resultSet;
    }

    public static String[][] getStudents(int num) {
        String[][] result = new String[num][7];
        for (int i = 0; i < num; i++) {
            result[i] = getStudent();
        }
        return result;
    }

    public static void main(String[] args) {
        getTestGrade(100000);
    }

    public static String[] getFacultyArrays() {
        return FACULTY;
    }

    public static String[] getMajorArrays(String faculty) {
        return MAJOR.get(faculty);
    }
}
